<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 插入区间 -->
    <script>
      var insert = function (intervals, newInterval) {
        let n = intervals.length
        intervals.push(newInterval)
        intervals.sort((a, b) => a[0] - b[0])
        let res = []
        let st = intervals[0][0],
          ed = intervals[0][1]
        for (let i = 1; i < n + 1; i++) {
          let left = intervals[i][0],
            right = intervals[i][1]
          if (ed < left) {
            res.push([st, ed])
            st = left
            ed = right
          } else {
            ed = Math.max(ed, right)
          }
        }
        res.push([st, ed])
        return res
      }
      console.log(
        insert(
          [
            [1, 3],
            [6, 9]
          ],
          [2, 5]
        )
      )
    </script>
  </body>
</html>
